package com.fingard.dsp.bank.directbank.citic05;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.citic.openbank.configuration.OpenBankSdkConfiguration;
import com.citic.openbank.sdk.API;
import com.citic.openbank.sdk.CiticSdk;
import com.citic.openbank.sdk.CiticSdkInterface;
import com.citic.openbank.sdk.RequestHeader;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IDeContract;
import com.fingard.dsp.bank.reqrespobj.contract.ContractState;
import com.fingard.dsp.bank.reqrespobj.decontract.ReqDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.RetAtsDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.detail.RetAtsDeContractDetail;
import com.fingard.text.StringHelper;


public class CITIC05DeContract extends CITIC05Base implements IDeContract {
    @Override
    public RetAtsDeContract deContract(ReqDeContract p_req) throws Exception {
        RetAtsDeContract retAts = new RetAtsDeContract(p_req.reqHeader);
        RetAtsDeContractDetail tmpRetDetail = new RetAtsDeContractDetail(p_req);

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req.contractDetail.recAct, "");
            if (actItem == null) {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "未找到账号[" + p_req.contractDetail.recAct + "]商户配置信息!";
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }
            CITIC05Header bankHeader = new CITIC05Header();
            bankHeader.openTransCode = "IFOPS015";
            bankHeader.openMerFlowId = p_req.contractDetail.askNumber;
            CiticSdkInterface sdk = CiticSdk.getInstance();
            setKeys(sdk, actItem);
            setConfiguration(sdk, actItem);
            RequestHeader header = new RequestHeader();
            writeCommonHeader(header, bankHeader, actItem);
            JSONObject jsonRequest = new JSONObject();
            jsonRequest.put("sndseqno", p_req.contractDetail.askNumber);
            jsonRequest.put("mccprtcno", actItem.recAgNum);
            jsonRequest.put("mrchname", actItem.userCode);
            jsonRequest.put("mrchtype", actItem.userFlag);
            jsonRequest.put("payname", p_req.contractDetail.payName);
            jsonRequest.put("payacc", p_req.contractDetail.payAct);
            jsonRequest.put("phone", p_req.contractDetail.cellPhone);
            jsonRequest.put("idtype", convertCertTypeToBank(p_req.contractDetail.certType));
            jsonRequest.put("idno", p_req.contractDetail.certNum);
            jsonRequest.put("protocolno", p_req.contractDetail.authCode);
            String bussinessJsonStr = jsonRequest.toJSONString();
            WriteBankLogLn2("加签加密前请求参数:\r\n" + bussinessJsonStr);

//            String tmpStrRet = sendToBank(header, jsonRequest.toJSONString());
            String tmpStrRet = sdk.remoteCall(header, jsonRequest.toJSONString());

            if (StringHelper.hasAnyChar(tmpStrRet)) {
                if (tmpStrRet.contains("{")) {
                    JSONObject tmpJsonResp = JSON.parseObject(tmpStrRet);
                    String retCode = tmpJsonResp.getString("RETCODE");
                    String retMsg = tmpJsonResp.getString("RETMSG");
                    tmpRetDetail.respCode = "0";
                    if("05".equals(retCode)){
                        tmpRetDetail.deSignState = ContractState.SUCCESS;
                    }else if("00".equals(retCode)){
                        tmpRetDetail.deSignState = ContractState.FAIL;
                    }else{
                        tmpRetDetail.deSignState = ContractState.SIGNING;
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = "银行响应数据格式非法";
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "银行响应为空";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsDeContract qryDeContract(ReqDeContract p_reqQry) throws Exception {
        return null;
    }
}